package co.yixiang.yshop.module.crm.dal.mysql.crmcustomer;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.mybatis.core.mapper.BaseMapperX;
import co.yixiang.yshop.framework.mybatis.core.query.LambdaQueryWrapperX;
import co.yixiang.yshop.module.crm.controller.admin.crmcustomer.vo.CrmCustomerPageReqVO;
import co.yixiang.yshop.module.crm.controller.admin.crmcustomer.vo.CrmCustomerRespVO;
import co.yixiang.yshop.module.crm.dal.dataobject.crmcustomer.CrmCustomerDO;
import co.yixiang.yshop.module.system.dal.dataobject.user.AdminUserDO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 客户 Mapper
 *
 * @author yshop
 */
@Mapper
public interface CrmCustomerMapper extends BaseMapperX<CrmCustomerDO> {

    default PageResult<CrmCustomerDO> selectPage(CrmCustomerPageReqVO reqVO, List<Long> ids) {
        MPJLambdaWrapper<CrmCustomerDO> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(CrmCustomerDO.class);

        if(ids!=null)
        {
            wrapper.in(CrmCustomerDO::getOwnerUserId, ids);
        }

        if (reqVO.getName() != null) {
            wrapper.like(CrmCustomerDO::getName, reqVO.getName());
        }
        if (reqVO.getMobile() != null) {
            wrapper.eq(CrmCustomerDO::getMobile, reqVO.getMobile());
        }
        if (reqVO.getTelephone() != null) {
            wrapper.eq(CrmCustomerDO::getTelephone, reqVO.getTelephone());
        }
        if (reqVO.getDealStatus() != null) {
            wrapper.eq(CrmCustomerDO::getDealStatus, reqVO.getDealStatus());
        }
        if (reqVO.getLevel() != null) {
            wrapper.eq(CrmCustomerDO::getLevel, reqVO.getLevel());
        }
        if (reqVO.getIndustry() != null) {
            wrapper.eq(CrmCustomerDO::getIndustry, reqVO.getIndustry());
        }
        if (reqVO.getSource() != null) {
            wrapper.eq(CrmCustomerDO::getSource, reqVO.getSource());
        }
        if (reqVO.getWeixin() != null) {
            wrapper.eq(CrmCustomerDO::getWeixin, reqVO.getWeixin());
        }
        if (reqVO.getQq() != null) {
            wrapper.eq(CrmCustomerDO::getQq, reqVO.getQq());
        }
        if (reqVO.getFollowStatus() != null) {
            wrapper.eq(CrmCustomerDO::getFollowStatus, reqVO.getFollowStatus());
        }
        if (reqVO.getNextTime() != null && reqVO.getNextTime().length == 2) {
            wrapper.between(CrmCustomerDO::getNextTime, reqVO.getNextTime()[0], reqVO.getNextTime()[1]);
        }
        if (reqVO.getFollowTime() != null && reqVO.getFollowTime().length == 2) {
            wrapper.between(CrmCustomerDO::getFollowTime, reqVO.getFollowTime()[0], reqVO.getFollowTime()[1]);
        }
        if (reqVO.getCollectTime() != null && reqVO.getCollectTime().length == 2) {
            wrapper.between(CrmCustomerDO::getCollectTime, reqVO.getCollectTime()[0], reqVO.getCollectTime()[1]);
        }
        //创建时间
        if (reqVO.getCreateTime() != null && reqVO.getCreateTime().length == 2) {
            wrapper.between(CrmCustomerDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        }
        //assignTime
        if (reqVO.getAssignTime() != null && reqVO.getAssignTime().length == 2) {
            wrapper.between(CrmCustomerDO::getAssignTime, reqVO.getAssignTime()[0], reqVO.getAssignTime()[1]);
        }
        if (reqVO.getChannelSource() != null) {
            wrapper.eq(CrmCustomerDO::getChannelSource, reqVO.getChannelSource());
        }
        if (reqVO.getSex() != null) {
            wrapper.eq(CrmCustomerDO::getSex, reqVO.getSex());
        }
        if (reqVO.getAge() != null) {
            wrapper.eq(CrmCustomerDO::getAge, reqVO.getAge());
        }
        if (reqVO.getIncome() != null) {
            wrapper.eq(CrmCustomerDO::getIncome, reqVO.getIncome());
        }
        //省份 城市 地区
        if(reqVO.getProvince() != null)
            wrapper.eq(CrmCustomerDO::getProvince, reqVO.getProvince());
        if(reqVO.getCity() != null)
            wrapper.eq(CrmCustomerDO::getCity, reqVO.getCity());
            if(reqVO.getArea() != null)
                wrapper.eq(CrmCustomerDO::getArea, reqVO.getArea());


        wrapper.orderByDesc(CrmCustomerDO::getId);

        return selectPage(reqVO, wrapper);
    }

    default PageResult<CrmCustomerRespVO> selectPage2(CrmCustomerPageReqVO reqVO, List<Long> ids) {
        MPJLambdaWrapper<CrmCustomerDO> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(CrmCustomerDO.class)
                .selectAs(AdminUserDO::getNickname, CrmCustomerRespVO::getOwnUserName)
                .selectAs(AdminUserDO::getNickname, CrmCustomerRespVO::getCreateName)
                .leftJoin(AdminUserDO.class, AdminUserDO::getId, CrmCustomerDO::getOwnerUserId)
                .leftJoin(AdminUserDO.class, AdminUserDO::getId, CrmCustomerDO::getCreator);

        if (reqVO.getName() != null) {
            wrapper.like(CrmCustomerDO::getName, reqVO.getName());
        }
        if (reqVO.getMobile() != null) {
            wrapper.eq(CrmCustomerDO::getMobile, reqVO.getMobile());
        }
        if (reqVO.getTelephone() != null) {
            wrapper.eq(CrmCustomerDO::getTelephone, reqVO.getTelephone());
        }
        if (reqVO.getWeixin() != null) {
            wrapper.eq(CrmCustomerDO::getWeixin, reqVO.getWeixin());
        }
        if (reqVO.getDealStatus() != null) {
            wrapper.eq(CrmCustomerDO::getDealStatus, reqVO.getDealStatus());
        }
        if (reqVO.getOwnUserId() != null) {
            wrapper.like(AdminUserDO::getNickname, reqVO.getOwnUserId());
        }
        if (reqVO.getCreateName() != null) {
            wrapper.like(AdminUserDO::getNickname, reqVO.getCreateName());
        }
        if (reqVO.getNextTime() != null && reqVO.getNextTime().length == 2) {
            wrapper.between(CrmCustomerDO::getNextTime, reqVO.getNextTime()[0], reqVO.getNextTime()[1]);
        }
        if (reqVO.getFollowTime() != null && reqVO.getFollowTime().length == 2) {
            wrapper.between(CrmCustomerDO::getFollowTime, reqVO.getFollowTime()[0], reqVO.getFollowTime()[1]);
        }
        if (reqVO.getCollectTime() != null && reqVO.getCollectTime().length == 2) {
            wrapper.between(CrmCustomerDO::getCollectTime, reqVO.getCollectTime()[0], reqVO.getCollectTime()[1]);
        }
        if (reqVO.getChannelSource() != null) {
            wrapper.eq(CrmCustomerDO::getChannelSource, reqVO.getChannelSource());
        }
        if (reqVO.getSex() != null) {
            wrapper.eq(CrmCustomerDO::getSex, reqVO.getSex());
        }
        if (reqVO.getAge() != null) {
            wrapper.eq(CrmCustomerDO::getAge, reqVO.getAge());
        }
        if (reqVO.getIncome() != null) {
            wrapper.eq(CrmCustomerDO::getIncome, reqVO.getIncome());
        }
        //省份 城市 地区
        if(reqVO.getProvince() != null)
            wrapper.eq(CrmCustomerDO::getProvince, reqVO.getProvince());
        if(reqVO.getCity() != null)
            wrapper.eq(CrmCustomerDO::getCity, reqVO.getCity());
        if(reqVO.getArea() != null)
            wrapper.eq(CrmCustomerDO::getArea, reqVO.getArea());


        wrapper.orderByDesc(CrmCustomerDO::getId);

        return selectJoinPage(reqVO, CrmCustomerRespVO.class, wrapper);
    }

}